// miniprogram/pages/interestRate/index.js
Page({
  data: {
    range: ['等额本息', '等额本金', '等本等息', '先息后本'],
    id: 0,
    lilv: ['年化利率', '月化利率', '日化利率'],
    lilvid: 0
  },
  onLoad: function (options) {},
  // 改变还款方式
  bindchange(e) {
    console.log("还款方式" + e.detail.value)
    this.setData({
      id: e.detail.value
    })
  },
  // 改变利率方式
  bindchangelilv(e) {
    this.setData({
      lilvid: e.detail.value,
      moneylilvValue: "",
      moneylilv: ""
    })
  },
  // 贷款金额值
  money(e) {
    this.setData({
      money: e.detail.value
    })
  },
  // 利率值（转换为月利率）
  moneylilv(e) {
    switch (this.data.lilv[this.data.lilvid]) {
      case "年化利率":
        this.setData({
          moneylilv: e.detail.value / 1.2
        });
        break;
      case "月化利率":
        this.setData({
          moneylilv: e.detail.value
        });
        break;
      case "日化利率":
        this.setData({
          moneylilv: e.detail.value * 30
        });
        break;
    }
    console.log(this.data.moneylilv)
  },
  // 贷款期限值
  moneydate(e) {
    this.setData({
      moneydate: e.detail.value
    })
  },
  // 计算（选择还款方式）
  calculation() {
    if (this.data.money == null || this.data.moneylilv == null || this.data.moneydate == null) {
      wx.showToast({
        title: '请输入完整数值',
        duration: 2000,
        icon: "none"
      })
    } else {
      console.log("选择还款方式")
      switch (this.data.range[this.data.id]) {
        case "等额本息":
          this.calculationBenxi();
          break;
        case "等额本金":
          this.calculationBenJin();
          break;
        case "等本等息":
          this.calculationDengBenxi();
          break;
        case "先息后本":
          this.calculationXiBen();
          break;
      }
    }
  },
  // 等额本息计算值（根据金额、月利率、期限计算）
  calculationBenxi() {
    console.log("计算等额本息")
    this.setData({
      table: []
    })
    var money = this.data.money
    var moneylilv = this.data.moneylilv
    var moneydate = this.data.moneydate
    // 月供
    var repayment = money * moneylilv / 1000 * Math.pow(1 + moneylilv / 1000, moneydate) / (Math.pow(1 + moneylilv / 1000, moneydate) - 1)
    var totalLixi = 0
    for (var i = 0; i < moneydate; i++) {
      // 每月本金
      var benjin = money * moneylilv / 1000 * Math.pow(1 + moneylilv / 1000, i) / (Math.pow(1 + moneylilv / 1000, moneydate) - 1)
      // 每月利息
      var lixi = repayment - benjin
      var table = []
      table.push('第' + (i + 1) + '期', repayment.toFixed(2), benjin.toFixed(2), lixi.toFixed(2))
      // 总利息
      totalLixi = totalLixi + lixi
      this.data.table.push(table)
    }
    console.log(this.data.table)
    this.setData({
      table: this.data.table,
      totalMoney: (parseFloat(money) + parseFloat(totalLixi)).toFixed(2),
      totalLixi: parseFloat(totalLixi).toFixed(2),
      totalBenjin: parseFloat(money).toFixed(2),
      totalDate: moneydate
    })
  },
  // 等额本金计算值（根据金额、月利率、期限计算）
  calculationBenJin() {
    console.log("计算等额本金")
    this.setData({
      table: []
    })
    var money = this.data.money
    var moneylilv = this.data.moneylilv
    var moneydate = this.data.moneydate
    var totalLixi = 0
    for (var i = 0; i < moneydate; i++) {
      // 每月本金
      var benjin = money / moneydate
      // 每月利息
      var lixi = (money - money / moneydate * i) * moneylilv / 1000
      // 月供
      var repayment = benjin + lixi
      var table = []
      table.push('第' + (i + 1) + '期', repayment.toFixed(2), benjin.toFixed(2), lixi.toFixed(2))
      totalLixi = totalLixi + lixi
      this.data.table.push(table)
    }
    this.setData({
      table: this.data.table,
      totalMoney: (parseFloat(money) + parseFloat(totalLixi)).toFixed(2),
      totalLixi: parseFloat(totalLixi).toFixed(2),
      totalBenjin: parseFloat(money).toFixed(2),
      totalDate: moneydate
    })
  },
  // 等本等息计算值（根据金额、月利率、期限计算）
  calculationDengBenxi() {
    console.log("计算等本等息")
    this.setData({
      table: []
    })
    var money = this.data.money
    var moneylilv = this.data.moneylilv
    var moneydate = this.data.moneydate
    var totalLixi = 0
    for (var i = 0; i < moneydate; i++) {
      // 每月本金
      var benjin = money / moneydate
      // 每月利息
      var lixi = money * moneylilv / 1000
      // 月供
      var repayment = benjin + lixi
      var table = []
      table.push('第' + (i + 1) + '期', repayment.toFixed(2), benjin.toFixed(2), lixi.toFixed(2))
      totalLixi = totalLixi + lixi
      this.data.table.push(table)
    }
    this.setData({
      table: this.data.table,
      totalMoney: (parseFloat(money) + parseFloat(totalLixi)).toFixed(2),
      totalLixi: parseFloat(totalLixi).toFixed(2),
      totalBenjin: parseFloat(money).toFixed(2),
      totalDate: moneydate
    })
  },
  // 先息后本计算值（根据金额、月利率、期限计算）
  calculationXiBen() {
    console.log("计算先息后本")
    this.setData({
      table: []
    })
    var money = this.data.money
    var moneylilv = this.data.moneylilv
    var moneydate = this.data.moneydate
    var totalLixi = 0
    for (var i = 0; i < moneydate; i++) {
      var benjin = 0
      if (i == moneydate - 1) {
        // 每月本金
        benjin = parseFloat(money)
      } else {
        benjin = 0
      }
      // 每月利息
      var lixi = money * moneylilv / 1000
      // 月供
      var repayment = benjin + lixi
      var table = []
      table.push('第' + (i + 1) + '期', repayment.toFixed(2), benjin.toFixed(2), lixi.toFixed(2))
      totalLixi = totalLixi + lixi
      this.data.table.push(table)
    }
    this.setData({
      table: this.data.table,
      totalMoney: (parseFloat(money) + parseFloat(totalLixi)).toFixed(2),
      totalLixi: parseFloat(totalLixi).toFixed(2),
      totalBenjin: parseFloat(money).toFixed(2),
      totalDate: moneydate
    })
  },
  onShareAppMessage() {
    return {
      title: "贷款利息计算",
      path: "/pages/calculationTools/pages/interestRate/index",
      imageUrl: '/images/function_icon/lilv.png',
    }
  },
  onShareTimeline() {
    return {
      title: '贷款利息计算',
      imageUrl: '/images/function_icon/lilv.png',
    }
  },
})